﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.DAL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MAS_WelfareFinances : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_HRM_WF_Finances fi = new B_HRM_WF_Finances();
        IBLL_FIM_MAS_Contacts con = new B_FIM_MAS_Contacts();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                //===========================

                DropDownList_Contact1.LoadControl(300, null);
                UpdateUI1("ViewAll");
            }
        }

        protected void LoadRadGrid1()
        {
            RadGrid1.DataSource = fi.GetFull();
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
            }

            cmdAddNew1.Enabled = false;
            cmdDelete1.Enabled = false;
        }

        protected void ClearControl1()
        {
        }

        //============================ control events ==============================

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            string strCommandName = e.CommandName;

            if (strCommandName == "GridDelete")
            {
                GridDataItem item = (GridDataItem)e.Item;

                string strFinanceSeq = main.WebString(item["finance_seq"].Text);
                //string strUserID = Session["EmpSeq"].ToString();
                fi.Delete(strFinanceSeq);
                
            }
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strFinanceSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["finance_seq"].Text;
                string strFinanceName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["finance_name"].Text;
                string strContactSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["contact_seq"].Text;
                string strRemarks = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["remarks"].Text;

                lblFinanceSeq.Text = main.WebString(strFinanceSeq);
                lblFinanceName.Text = main.WebString(strFinanceName);
                DropDownList_Contact1.SetSelectedValue(main.WebString(strContactSeq));
                lblRemarks.Text = main.WebString(strRemarks);
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {

        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            M_HRM_WF_Finances info = new M_HRM_WF_Finances();
            string strFinanceSeq = lblFinanceSeq.Text;
            string strContactSeq = DropDownList_Contact1.GetSelectedValue();
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            if (strContactSeq.Length == 0)
            {
                imgContactSeq.Visible = true;
                return;
            }
            else
            {
                imgContactSeq.Visible = false;
            }

            info.finance_seq = Convert.ToInt32(strFinanceSeq);
            info.contact_seq = main.ConvertNullInt(strContactSeq);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = dtNow;

            fi.Update(info);

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Update");
            ClearControl1();
        }
        
        //==========================================================================
    }
}
